package com.zzb.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.CompanyVO;
import com.zzb.service.CompanyService;
import com.zzb.service.impl.CompanyServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/company/update")
public class CompanyUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //得到前端数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        String logo = req.getParameter("logo");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String net = req.getParameter("net");
        String address = req.getParameter("address");
        String version = req.getParameter("version");
        String linePhone = req.getParameter("linePhone");
        String ad = req.getParameter("ad");

        //对前端数据进行封装
        CompanyVO companyVO = new CompanyVO();
        companyVO.setId(Integer.valueOf(id));
        companyVO.setName(Tool.nullToStr(name));
        companyVO.setContent(Tool.nullToStr(content));
        companyVO.setRemark(Tool.nullToStr(remark));
        companyVO.setLogo(Tool.nullToStr(logo));
        companyVO.setEnable(Integer.valueOf(Tool.nullToNum(enable)));
        companyVO.setPhone(Tool.nullToStr(phone));
        companyVO.setEmail(Tool.nullToStr(email));
        companyVO.setNet(Tool.nullToStr(net));
        companyVO.setAddress(Tool.nullToStr(address));
        companyVO.setVersion(Tool.nullToStr(version));
        companyVO.setLinePhone(Tool.nullToStr(linePhone));
        companyVO.setAd(Tool.nullToStr(ad));

        //调用服务逻辑
        CompanyService companyService = new CompanyServiceImpl();

        //得到返回值
        ResultDTO resultDTO = companyService.update(companyVO);
        //响应
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
